import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useUserStore } from '@/stores/counter'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('../views/AdminView.vue'),
      children:[
        {
          path:'index',
          component: () => import("../views/admin/index.vue")
        },
        {
          path:'user',
          component: () => import("../views/admin/userView.vue")
        },
        {
          path:'order',
          component: () => import("../views/admin/orderView.vue")
        },
        {
          path:'phone',
          component: () => import("../views/admin/phoneView.vue")
        },
      ]
    },
    {
      path: '/phone',
      name: 'phone',
      component: () => import('../views/PhoneView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    }
  ]
})
router.beforeEach((to,from) => {
  const store = useUserStore()
  console.log(to.path);
  if((to.path === '/phone' || to.path.includes("admin"))&& !store.isLogin()){
   return { name:'login' }
  }
  console.log('yesyes');
  return true
  
})
export default router
